const User = require('../../login/lib/userSchema');
const OilLog = require('../../../domain/oilLog');
const AchementApply = require('../../../domain/achievementApply');
const utils = require('../../../util/utils');
const withdraw = require('../../../domain/withdraw');
const uuid = require('node-uuid');

//查询徒弟数量
exports.sonCount = function (uid) {
    return new Promise((resolve, reject) => {
        User.count({parent_id: uid, is_effect_user: true}).exec(function (err, count) {
            if (err) return reject(err);
            resolve(parseInt(count));
        });
    });
}

/**
 * 插入成就任务完成申请
 * @param uid
 * @param mission
 */
exports.insertAchievement = function (uid, task_name, mission) {
    let achievement = new AchementApply({
        aid: utils.genUuid(utils.consts.TYPE_RAND),
        uid: uid,
        task_name: task_name,
        task: mission,
        status: 1,
        gmt_create: new Date().getTime(),
        gmt_modified: new Date().getTime()
    });
    achievement.save(function () {
    });
}

//获取昨日石油获取总量
exports.getDailyOil = function (uid, begin, end) {
    return new Promise((resolve, reject) => {
        OilLog.aggregate([
            {
                $match: {
                    'uid': uid,
                    'operate': 'add',
                    'gmt_create': {'$gte': begin, '$lt': end},
                    'type': {'$ne': 'give_oil'}
                }
            },
            {
                $group: {_id: null, num_tutorial: {$sum: '$increase_oil'}}
            }
        ], function (err, total) {
            console.log('total')
            console.log(total)
            if (err) return reject(err);
            if (total.length > 0) {
                resolve(total[0].num_tutorial);
            } else {
                resolve(0);
            }
        });
    });
}

//昨日全服石油总量
exports.getDailyTotalOil = function (begin, end) {
    return new Promise((resolve, reject) => {
        OilLog.aggregate([
            {
                $match: {'operate': 'add', 'gmt_create': {'$gte': begin, '$lt': end}, 'type': {'$ne': 'give_oil'}}
            },
            {
                $group: {_id: null, num_tutorial: {$sum: '$increase_oil'}}
            }
        ], function (err, total) {
            if (err) return reject(err);
            if (total.length > 0) {
                resolve(total[0].num_tutorial);
            } else {
                resolve(0);
            }
        });
    });
}

/**
 * 保存每日0.3元提现申请
 * @param uid
 */
exports.saveDailyWithdraw = function (uid, real_open_id, total_withdraw, total_money) {
    //插入提现记录
    let Withdraw = new withdraw({
        wid: uuid.v4(),
        uid: uid,
        real_open_id: real_open_id,
        profits: total_money,
        total_withdraw: total_withdraw,
        money: 0.3,
        status: 2,
        super_status: 1,
        gmt_create: new Date().valueOf(),
        gmt_modified: new Date().valueOf()
    });
    Withdraw.save(Withdraw, function (err, data) {
        if (!!err) {
            console.error('提现记录 保存失败: ' + err);
        }
    });
}

/**
 * 有徒弟的用户
 */
exports.haveSonUser = function () {
    return new Promise((resolve, reject) => {
        User.find({'apprentice_num': {'$gt': 1}}, function (err, users) {
            if (err) return reject(err);
            resolve(users);
        }).sort({'apprentice_num': -1});
    });
}